package
{
	import flash.events.Event;
	import flash.filters.GlowFilter;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	
	import org.flixel.*;
	import org.flixel.system.FlxAnim;

	public class ModalSubmitPopup extends FlxGroup
	{
		protected static const EASY:int = 0;
		protected static const MEDIUM:int = 1;
		protected static const HARD:int = 2;
		
		protected var curDiff:int;
		
		protected var submittedName:TextField;
		
		protected var easy:FlxSprite;
		protected var medium:FlxSprite;
		protected var hard:FlxSprite;
		protected var submitButton:FlxSprite;
		protected var cancelButton:FlxSprite;
		protected var nameBG:FlxSprite;
		
		protected var name:String;
		
		protected var difficultySelected:Boolean;
		protected var validated:Boolean;
		protected var selectFilter:GlowFilter;
		
		protected var currentDif:FlxSprite;
		
		protected var spec:PuzzleSpecification;
		
		protected var submittedMap:Boolean;
		protected var done:Boolean;
		
		protected var preSubmitStuff:FlxGroup;
		
		protected var postSubmitStuff:FlxGroup;
		protected var duplicateSubmitStuff:FlxGroup;
		protected var errorSubmitStuff:FlxGroup;
		protected var okButton:FlxSprite;
		
		protected var dupName:FlxText;
		
		protected var tries:int;
		protected var submitting:Boolean;
		
		public function ModalSubmitPopup(mapSpec:PuzzleSpecification)
		{
			tries = 5;
			this.add(new FlxSprite(83,113,Res.popupBackground));
			
			preSubmitStuff = new FlxGroup();
			postSubmitStuff = new FlxGroup();
			duplicateSubmitStuff = new FlxGroup();
			errorSubmitStuff = new FlxGroup();
			
			
			spec = mapSpec;
			
			var titleText:FlxText = new FlxText(107, 137, 300, "Final Settings", false);
			titleText.setFormat("Arial", 30, 0x111111, "center");
			preSubmitStuff.add(titleText);
			var sub:FlxText = new FlxText(107, 200, 300, "projected difficult: select one", false);
			sub.setFormat("Arial", 20, 0x111111, "center");
			preSubmitStuff.add(sub);
			
			easy = new FlxSprite(98, 230, Res.easyOption);
			medium = new FlxSprite(208, 230, Res.mediumOption);
			hard = new FlxSprite(318, 230 , Res.hardOption);
			selectFilter = new GlowFilter(0xDD22DD/*0xe5cda4*/, 0.8, 18, 18, 3,1,true);
			
			currentDif = new FlxSprite();

			preSubmitStuff.add(easy);
			preSubmitStuff.add(medium);
			preSubmitStuff.add(hard);
			
			var n:FlxText = new FlxText(107, 300, 300, "name for submitted puzzle:", false);
			n.setFormat("Arial", 20, 0x111111, "center");
			preSubmitStuff.add(n);
			
			submitButton = new FlxSprite(208, 380, Res.disabledFinalSubmit);
			preSubmitStuff.add(submitButton);
			
			cancelButton = new FlxSprite(345, 425, Res.cancelButton);
			preSubmitStuff.add(cancelButton);
			
			nameBG = new FlxSprite(107,325,Res.saveBG);
			preSubmitStuff.add(nameBG);
			
			submittedName = new TextField();
			submittedName.type = TextFieldType.INPUT;
			submittedName.x = 117;
			submittedName.y = 330;
			submittedName.width = 280;
			submittedName.height = 30;
			submittedName.maxChars = 20;
			submittedName.multiline = false;
			submittedName.restrict = "a-zA-Z0-9";
			submittedName.text = new String("untitled");
			submittedName.setTextFormat(new TextFormat("Arial", 24, 0x000000));
			submittedName.addEventListener(Event.CHANGE, updateName);
			
			name = "";
			
			okButton = new FlxSprite(220, 425, Res.okButton);
			//post stuff
			var complete:FlxText = new FlxText(107, 237, 300, "Puzzle Submitted!", false);
			complete.setFormat("Arial", 36, 0x111111, "center");
			postSubmitStuff.add(complete);
			
			postSubmitStuff.add(okButton);
			
			//duplicate
			var dup:FlxText = new FlxText(107, 137, 300, "Puzzle already submitted under name:", false);
			dup.setFormat("Arial", 36, 0x111111, "center");
			dupName = new FlxText(107, 300, 300, "", false);
			dupName.setFormat("Arial", 36, 0x111111, "center");
			dupName.color = 0x0000FF;
			duplicateSubmitStuff.add(dup);
			duplicateSubmitStuff.add(dupName);
			
			duplicateSubmitStuff.add(okButton);
			
			//error
			var err:FlxText = new FlxText(107, 137, 300, "Unknown Error Submitting Puzzle.", false);
			err.setFormat("Arial", 36, 0x111111, "center");
			err.color = 0xFF0000;
			errorSubmitStuff.add(err);
			
			errorSubmitStuff.add(okButton);
			
			this.add(preSubmitStuff);
			
			
		}
		
		public override function update():void  {
			super.update();
			
			if (FlxG.mouse.justPressed()) {
				checkDifficulty();
				checkSubmit();
				checkCancel();
				if (submittedMap) {
					checkOk();
				}
			}
			if (name.length > 0 && difficultySelected && !validated) {
				submitButton.loadGraphic(Res.activeFinalSubmit, false, false, 100, 50, false);
				validated = true;
			}
			if (name.length == 0 && validated) {
				submitButton.loadGraphic(Res.disabledFinalSubmit, false, false, 100, 50, false);
				validated = false;
			}
		}
		
		private function checkOk():void {
			if (okButton.overlapsPoint(FlxG.mouse.x, FlxG.mouse.y)) {
				done = true;
			}
		}
		
		private function checkDifficulty():void {
			if (easy.overlapsPoint(FlxG.mouse.x, FlxG.mouse.y)) {
				difficultySelected = true;
				currentDif.removeFilter();
				currentDif = easy;
				easy.setFilter(selectFilter);
				curDiff = EASY;
			} else if (medium.overlapsPoint(FlxG.mouse.x, FlxG.mouse.y)) {
				difficultySelected = true;
				currentDif.removeFilter();
				currentDif = medium;
				medium.setFilter(selectFilter);
				curDiff = MEDIUM;
			} else if (hard.overlapsPoint(FlxG.mouse.x, FlxG.mouse.y)) {
				difficultySelected = true;
				currentDif.removeFilter();
				currentDif = hard;
				hard.setFilter(selectFilter);
				curDiff = HARD;
			}
		}
		
		private function checkSubmit():void {
			if (!submitting && validated && submitButton.overlapsPoint(FlxG.mouse.x, FlxG.mouse.y)) {
				var tmpArrows:Array = spec.getArrows();
				var tmpChicks:Array = spec.getChickens();
				Client.submitPuzzle(spec.getMap(),name,spec.getTileSet(), tmpArrows[0], tmpArrows[1], tmpArrows[2], tmpArrows[3], tmpChicks.length,curDiff, getReply); 
				submittedMap = true;
				submitting = true;
			}
		}
		
		private function checkCancel():void {
			if (cancelButton.overlapsPoint(FlxG.mouse.x, FlxG.mouse.y)) {
				done = true;
			}
		}
		
		private function updateName(e:Event):void {
			name = submittedName.text;
		}
		
		public function displayNameText():void {
			FlxG.stage.addChild(submittedName);
		}
		
		public function hideNameText():void {
			FlxG.stage.removeChild(submittedName);
		}
		
		public function finishedPopup():Boolean {
			return done;
		}
		
		public function submitted():Boolean {
			return submittedMap;
		}
		
		/**
		 * Called if puzzle submit successful
		 */
		private function getReply(e:Event):void {
			var reply:String = e.target.data;
			trace("returned " + e.target.data);
			
			if (reply == "We've entered your entry successfully!") {
				this.remove(preSubmitStuff);
				this.add(postSubmitStuff);
				FlxG.stage.removeChild(submittedName);
				Elemental.Log.logMapCreated(name);
				if (Elemental.MODE == Elemental.FACEBOOK_MODE) {
					Elemental.achievements.updateCreatePuzzle();
				}
			} else if (reply.indexOf(":") == 22) { // "Duplicate map detected: "{
				var nameArray:Array = reply.split(": ");
				this.remove(preSubmitStuff);
				if (nameArray.length > 0)
					dupName.text = nameArray[1];
				this.add(duplicateSubmitStuff);
				FlxG.stage.removeChild(submittedName);
			} else {
				this.remove(preSubmitStuff);
				this.add(errorSubmitStuff);
				FlxG.stage.removeChild(submittedName);
			}
		}
	}
}